﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication3
{
    public partial class FormStuInfo : FormBase
    {
        public FormStuInfo()
        {
            InitializeComponent();
        }

        public FormStuInfo(ToolStripMenuItem tsMi):base(tsMi)
        {
            InitializeComponent();
        }

        private void FormStuInfo_Load(object sender, EventArgs e)
        {
            SqlConnection conn = null;
            SqlDataReader reader = null;
            try
            {
                conn = new SqlConnection();
                //指定连接串
                conn.ConnectionString = "Data Source=.;Initial Catalog=master;Integrated Security=True";
                //打开连接
                conn.Open();
                //创建命令对象
                SqlCommand cmd = new SqlCommand();
                //指定连接对象
                cmd.Connection = conn;
                //指定命令类型

                cmd.CommandType = CommandType.Text;  //可以省略
                //cmd.CommandText = "select * from useraccount where username = '" + tbUser.Text + "' and userpassword = '" + tbPwd.Text + "'";
                //string fmt = "select * from useraccount where username = '{0}' and userpassword = '{1}'";
                //cmd.CommandText = string.Format(fmt, tbUser.Text, tbPwd.Text);
                cmd.CommandText = @"select * from useraccount where 
                username = @name and userpassword = @pwd;select * from menu where pmenuid = @id;
                select * from tpstu where sno like @no";
                //准备参数
                SqlParameter param = new SqlParameter();
                param.ParameterName = "@name";
                param.Value = "0000776";
                cmd.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@pwd";
                param.Value = "1";
                cmd.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@id";
                param.Value = "100000079";
                cmd.Parameters.Add(param);

                param = new SqlParameter();
                param.ParameterName = "@no";
                param.Value = "20151080404%";
                cmd.Parameters.Add(param);

                reader = cmd.ExecuteReader();

                while (true)
                {
                    //指向下一条记录
                    while (reader.Read())
                    {
                        string fmt = "{0}   {1}  {2}   {3}";
                        listBox1.Items.Add(string.Format(fmt, reader[0], reader[1], reader[2], reader[3]));
                    }
                    if (!reader.NextResult())
                        break;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                if (conn != null && conn.State == ConnectionState.Open)
                    conn.Close();
            }
        }
    }
}
